package com.xiongwei.prototype;

import com.xiongwei.prototype.deepclone.DeepCloneableTarget;
import com.xiongwei.prototype.deepclone.DeepProtoType;

/**
 * @author XiongWei
 * @date 2023/12/18
 */
public class Client {

    public static void main(String[] args) throws Exception {

        Sheep sheep  = new Sheep("abc", 2);
        sheep.friend = new Sheep("efg", 3);
        //浅拷贝
        Sheep clone1 = sheep.clone();
        Sheep clone2 = sheep.clone();
        Sheep clone3 = sheep.clone();
        System.out.println("sheep:" + sheep + ",sheep.friend:" + sheep.friend.hashCode());
        System.out.println("clone1:" + clone1 + ",clone1.friend:" + clone1.friend.hashCode());
        System.out.println("clone2:" + clone2 + ",clone2.friend:" + clone2.friend.hashCode());
        System.out.println("clone3:" + clone3 + ",clone3.friend:" + clone3.friend.hashCode());

        //clone方式深拷贝
        DeepProtoType dpt = new DeepProtoType();
        dpt.setName("张三");
        dpt.setDeepCloneableTarget(new DeepCloneableTarget("name","class"));

        DeepProtoType deepClone1 = dpt.clone();
        DeepProtoType deepClone2 = dpt.clone();
        System.out.println("deepClone1.name=" + deepClone1.getName() + ",deepClone1.deepCloneableTarget=" + deepClone1.getDeepCloneableTarget().hashCode());
        System.out.println("deepClone2.name=" + deepClone2.getName() + ",deepClone2.deepCloneableTarget=" + deepClone2.getDeepCloneableTarget().hashCode());

        //序列化方式深拷贝
        DeepProtoType dpt2 = new DeepProtoType();
        dpt.setName("李四");
        dpt.setDeepCloneableTarget(new DeepCloneableTarget("name2","class2"));

        DeepProtoType deepClone3 = dpt.clone();
        DeepProtoType deepClone4 = dpt.clone();
        System.out.println("deepClone3.name=" + deepClone1.getName() + ",deepClone3.deepCloneableTarget=" + deepClone3.getDeepCloneableTarget().hashCode());
        System.out.println("deepClone4.name=" + deepClone2.getName() + ",deepClone4.deepCloneableTarget=" + deepClone4.getDeepCloneableTarget().hashCode());

    }
}
